//
//  FourthViewController2.m
//  UIhomework_14
//
//  Created by dllo on 16/1/2.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "FourthViewController2.h"
#import "FourthViewController3.h"
#import "DataBaseHandle.h"
#import "RegisterModel.h"
@interface FourthViewController2 ()<UITextFieldDelegate>
@property (nonatomic, retain) UITextField *tf;
@property (nonatomic, retain) UITextField *tf2;
@end

@implementation FourthViewController2

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
-(void)dealloc{
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    self.navigationItem.title = @"用户登入";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 100, 20)];
    label.text = @"用户名:";
    [self.view addSubview:label];
    [label release];
    
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(120, 30, 200, 20)];
    _tf.placeholder = @"请输入用户名";
    _tf.layer.cornerRadius = 5;
    _tf.layer.borderWidth = 1;
    [self.view addSubview:_tf];
    _tf.delegate = self;
    [_tf release];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 100, 20)];
    label2.text = @"密码:";
    [self.view addSubview:label2];
    [label2 release];
    
    self.tf2 = [[UITextField alloc] initWithFrame:CGRectMake(120, 70, 200, 20)];
    _tf2.placeholder = @"输入密码";
    _tf2.secureTextEntry = YES;
    _tf2.layer.cornerRadius = 5;
    _tf2.layer.borderWidth = 1;
    [self.view addSubview:_tf2];
    _tf2.delegate = self;
    [_tf2 release];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(20, 120, 150, 30);
    [button1 setTitle:@"登陆" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor greenColor];
    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(200, 120, 150, 30);
    [button2 setTitle:@"注册" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor orangeColor];
    [button2 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)itemAction:(UIBarButtonItem *)itemAction{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)buttonAction:(UIButton *)button{
    NSMutableArray *arr = [[DataBaseHandle shareDataBaseHandle] selectRegister];
    int i = -1;
    for (RegisterModel *model in arr) {
        ++i;
        if (model.username == _tf.text && model.password == _tf2.text) {
            
            [self.delegate chuanzhi];
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];

        }
        if (i == arr.count - 1){
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆失败" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];

        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)buttonAction1:(UIButton *)button{
    FourthViewController3 *VC = [[FourthViewController3 alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    [VC release];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
